********************************************************************************
********************************************************************************
* Table 3. Aggregate estimates - first differences
********************************************************************************
********************************************************************************
clear
use "$data/fdiff.dta", clear
set more off

********************************************************************************
* NET FISCAL CONTRIBUTIONS
********************************************************************************

* DEFINE VARIABLES
global y1 	totnet_all_res_d
global y2 	totnet_excl_res_d
global x 	migch_fd
global z	IV2005
global w 	totpop13_wa

* CREATE CONTROLS
global ctrl feff sh_pgds sh_wap13 sh_college13 sh_manuf13
global ctrlxyear c.feff#i.year c.sh_pgds#i.year c.sh_wap13#i.year ///
				 c.sh_college13#i.year c.sh_manuf13#i.year

* All budget & using population in t-1

	// OLS regressions
qui areg $y1 $x [aw=$w], absorb(year) robust
	estimates store ols1, title (OLS)
	estadd local year_fe "Yes"
	estadd local ctrl ""
	estadd local ctrlxyear ""

qui areg $y1 $x $ctrl [aw=$w], absorb(year) robust
	estimates store ols2, title (OLS)
	estadd local year_fe "Yes"
	estadd local ctrl "Yes"
	estadd local ctrlxyear ""
	
qui areg $y1 $x $ctrlxyear [aw=$w], absorb(year) robust
	estimates store ols3, title (OLS)
	estadd local year_fe "Yes"
	estadd local ctrl ""
	estadd local ctrlxyear "Yes"
	
	// 2SLS regressions: 2005
ssc install ivreg2, replace
ssc install ranktest, replace 

qui ivreg2 $y1 ($x = $z) i.year [aw=$w], robust first small
	estimates store iv1, title (2SLS-2005)
	estadd local year_fe "Yes"
	estadd local ctrl ""
	estadd local ctrlxyear ""
	matrix list e(first)				// Store results for tables
	matrix first = e(first)
	estadd scalar Fstat = first[4,1]	// Kleiberger-paap rk Wald F statistics (equivalent to Sanderson-Windmeijer F stat when there is one endonegous regressor)
	estadd scalar ARtest = e(arfp)		// Anderson-Rubin weak instrument test (p-value)

qui ivreg2 $y1 ($x = $z) $ctrl i.year [aw=$w], robust first small
	estimates store iv2, title (2SLS-2005)
	estadd local year_fe "Yes"
	estadd local ctrl "Yes"
	estadd local ctrlxyear ""
	matrix list e(first)					
	matrix first = e(first)
	estadd scalar Fstat = first[4,1]
	estadd scalar ARtest = e(arfp)

qui ivreg2 $y1 ($x = $z) $ctrlxyear i.year [aw=$w], robust first small
	estimates store iv3, title (2SLS-2005)
	estadd local year_fe "Yes"
	estadd local ctrl ""
	estadd local ctrlxyear "Yes"
	matrix list e(first)					
	matrix first = e(first)
	estadd scalar Fstat = first[4,1]
	estadd scalar ARtest = e(arfp)

	// Tabulate results
esttab ols1 ols2 ols3 iv1 iv2 iv3,  keep($x) ///
star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3)  ///
coeflabels($x "Immigrant inflows") ///
mlabels( , titles) collabels(none) obslast varwidth(20)  modelwidth(15) ///
scalars("Fstat Kleibergen-Paap F" "ARtest Anderson-Rubin test" ///
"year_fe Year FE" "ctrl MSA-level controls" "ctrlxyear Controls x Year FE") ///
title("All budget") 

* Restricted budget & using population in t-1

	// OLS regressions
qui areg $y2 $x [aw=$w], absorb(year) robust
	estimates store ols4, title (OLS)
	estadd local year_fe "Yes"
	estadd local ctrl ""
	estadd local ctrlxyear ""

qui areg $y2 $x $ctrl [aw=$w], absorb(year) robust
	estimates store ols5, title (OLS)
	estadd local year_fe "Yes"
	estadd local ctrl "Yes"
	estadd local ctrlxyear ""
	
qui areg $y2 $x $ctrlxyear [aw=$w], absorb(year) robust
	estimates store ols6, title (OLS)
	estadd local year_fe "Yes"
	estadd local ctrl ""
	estadd local ctrlxyear "Yes"
	
	// 2SLS regressions: 2005
qui ivreg2 $y2 ($x = $z) i.year [aw=$w], robust first small
	estimates store iv4, title (2SLS-2005)
	estadd local year_fe "Yes"
	estadd local ctrl ""
	estadd local ctrlxyear ""
	matrix list e(first)					
	matrix first = e(first)
	estadd scalar Fstat = first[4,1]
	estadd scalar ARtest = e(arfp)

qui ivreg2 $y2 ($x = $z) $ctrl i.year [aw=$w], robust first small
	estimates store iv5, title (2SLS-2005)
	estadd local year_fe "Yes"
	estadd local ctrl "Yes"
	estadd local ctrlxyear ""
	matrix list e(first)					
	matrix first = e(first)
	estadd scalar Fstat = first[4,1]
	estadd scalar ARtest = e(arfp)

qui ivreg2 $y2 ($x = $z) $ctrlxyear i.year [aw=$w], robust first small
	estimates store iv6, title (2SLS-2005)
	estadd local year_fe "Yes"
	estadd local ctrl ""
	estadd local ctrlxyear "Yes"
	matrix list e(first)					
	matrix first = e(first)
	estadd scalar Fstat = first[4,1]
	estadd scalar ARtest = e(arfp)

	// Tabulate results
esttab ols4 ols5 ols6 iv4 iv5 iv6,  keep($x) ///
star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3)  ///
coeflabels($x "Immigrant inflows") ///
mlabels( , titles) collabels(none) obslast varwidth(20)  modelwidth(15) ///
scalars("Fstat Kleibergen-Paap F" "ARtest Anderson-Rubin test" ///
"year_fe Year FE" "ctrl MSA-level controls" "ctrlxyear Controls x Year FE") ///
title("Restrictive budget") 
